package com.ruoyi.energy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.TcCar;
import com.ruoyi.energy.domain.vo.TcCarWXVO;
import com.ruoyi.energy.service.ITcCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 内部车辆Controller
 *
 * @author cjie
 * @date 2021-11-30
 */
@Api(tags = " 内部车辆")
@RestController
@RequestMapping("/tcCar")
public class TcCarController extends BaseController {
    @Autowired
    private ITcCarService tcCarService;

    /**
     * 查询内部车辆列表
     */
    @ApiOperation(value="查询内部车辆列表", notes="查询内部车辆列表")
    @PreAuthorize(hasPermi = "energy:tcCar:list")
    @GetMapping("/list")
    public TableDataInfo list(TcCar tcCar) {
        startPage();
        List<TcCar> list = tcCarService.selectTcCarList(tcCar);
        return getDataTable(list);
    }

    /**
     * 微信查询内部车辆列表
     */
    @ApiOperation(value="微信查询内部车辆列表", notes="微信查询内部车辆列表")
    @PreAuthorize(hasPermi = "energy:tcCar:list")
    @GetMapping("/wx/list")
    public TableDataInfo wxlist(TcCarWXVO tcCar) {
        startPage();

        TcCar search = BeanUtil.toBean(tcCar, TcCar.class);
        List<TcCar> tcCars = tcCarService.selectTcCarList(search);
        TableDataInfo tableData = getDataTable(tcCars);
        List<TcCarWXVO> results = new ArrayList<>(tcCars.size());
        for (TcCar tcar : tcCars) {
            results.add(BeanUtil.toBean(tcar, TcCarWXVO.class));
        }
        tableData.setRows(results);

        return tableData;
    }

    /**
     * 导出内部车辆列表
     */
    @ApiOperation(value="导出内部车辆列表", notes="导出内部车辆列表")
    @PreAuthorize(hasPermi = "energy:tcCar:export")
    @Log(title = "内部车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TcCar tcCar) throws IOException {
        List<TcCar> list = tcCarService.selectTcCarList(tcCar);
        ExcelUtil<TcCar> util = new ExcelUtil<TcCar>(TcCar.class);
        util.exportExcel(response, list, "内部车辆数据");
    }
    /**
     * 获取内部车辆详细信息
     */
    @ApiOperation(value="获取内部车辆详细信息", notes="获取内部车辆详细信息")
    @PreAuthorize(hasPermi = "energy:tcCar:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tcCarService.selectTcCarById(id));
    }

    /**
     * 同步内部车辆信息
     */
    @ApiOperation(value="同步内部车辆信息", notes="同步内部车辆信息")
    @PreAuthorize(hasPermi = "energy:tcCar:edit")
    @GetMapping(value = "/syncData/{id}")
    public AjaxResult syncData(@PathVariable("id")String id) {
        return tcCarService.syncData(id);
    }



    /**
     * 新增内部车辆
     */
    @ApiOperation(value="新增内部车辆", notes="新增内部车辆")
    @PreAuthorize(hasPermi = "energy:tcCar:add")
    @Log(title = "内部车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcCar tcCar) {
        return toAjax(tcCarService.insertTcCar(tcCar));
    }

    /**
     * 修改内部车辆
     */
    @ApiOperation(value="修改内部车辆", notes="修改内部车辆")
    @PreAuthorize(hasPermi = "energy:tcCar:edit")
    @Log(title = "内部车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TcCar tcCar) {
        return toAjax(tcCarService.updateTcCar(tcCar));
    }

    /**
     * 删除内部车辆
     */
    @ApiOperation(value="删除内部车辆", notes="删除内部车辆")
    @ApiImplicitParam(name = "ids", value = "内部车辆ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:tcCar:remove")
    @Log(title = "内部车辆", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tcCarService.deleteTcCarByIds(ids));
    }
}
